//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Verifications.Implementations
{
    using System;
    using Preflight.Model;

    public class ConfigurationVerification : VerificationBase<ConfigurationVerificationDescription>
    {
        protected override bool OnBeforeVerification(VerificationResult result)
        {
            return true;
        }

        protected override void AddVerificationDetail(VerificationResult result)
        {
            result.AddMessage(MessageType.Information, "Verifying configuration value for {0}", this.Description.ConfigurationKey);
        }

        protected override void ExecuteVerification(VerificationResult result)
        {
            string value;

            if (!this.TryGetConfigurationValue(result, out value))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                result.AddMessage(MessageType.Error, "{0} has a null or empty value.", this.Description.ConfigurationKey);
                return;
            }

            this.TryConvertingValue(result, value);
        }

        private void TryConvertingValue(VerificationResult result, string value)
        {
            try
            {
                Func<string, bool> convertFunc = 
                    this.Description.CanBeConvertedTo ?? 
                    (actual => Convert.ChangeType(value, this.Description.ExpectedType) != null);
                
                if (convertFunc(value) == false)
                {
                    result.AddMessage(MessageType.Error, "{0} cannot be converted to target type", value);
                }
            }
            catch (Exception exception)
            {
                result.AddMessage(MessageType.Error, "Could not convert value");
                result.AddException(exception);
            }
        }

        private bool TryGetConfigurationValue(VerificationResult result, out string value)
        {
            try
            {
                value = this.Description.GetConfiguration(this.Description.ConfigurationKey);
                result.AddMessage(MessageType.Information, "Configuration value: {0}", value);
                return true;
            }
            catch (Exception exception)
            {
                result.AddMessage(MessageType.Error, "Could not read configuration value");
                result.AddException(exception);
                value = null;
                return false;
            }
        }
    }
}
